import { ref } from 'vue'
import { getSparePartApplyList as getSparePartApplyListAPI } from '@/api/store'

/**
 * 配件申请单列表服务
 */
export default function () {
  const getSparePartApplyListLoading = ref(false)
  const total = ref(0)
  const getSparePartApplyListParam = ref<StorageAPIRequest.GetSparePartApplyListParam>({
    code: '',
    limit: 50,
    networkCode: '',
    networkName: '',
    organizeId: '',
    page: 1,
    status: '',
    startTime: '',
    endTime: '',
  })
  const sparePartApplyList = ref<Array<WarehouseAPIResponse.SparePartApplyListItem>>([])

  const resetGetSparePartApplyListParam = () => {
    getSparePartApplyListParam.value = {
      code: '',
      limit: 50,
      networkCode: '',
      networkName: '',
      organizeId: '',
      page: 1,
      status: '',
      startTime: '',
      endTime: '',
    }
  }

  const getSparePartApplyList = async () => {
    try {
      getSparePartApplyListLoading.value = true
      const res = await getSparePartApplyListAPI(getSparePartApplyListParam.value)
      getSparePartApplyListLoading.value = false
      if (res.data.code === 20000) {
        total.value = Number(res.data.data.total)
        sparePartApplyList.value = res.data.data.rows
        return ''
      }

      return res.data.message
    } catch (e) {
      getSparePartApplyListLoading.value = false
      return '获取配件申请单列表失败，请稍后重试'
    }
  }

  return {
    total,
    sparePartApplyList,
    getSparePartApplyListParam,
    getSparePartApplyListLoading,
    getSparePartApplyList,
    resetGetSparePartApplyListParam,
  }
}
